package com.open.party.cloud.viewModel;

import android.util.Log
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.alibaba.fastjson.JSON
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.CommVo
import com.open.party.cloud.model.FreePhotosBean
import com.open.party.cloud.model.FreePhotosVo
import com.open.party.cloud.model.JiFenBean
import com.open.party.cloud.model.data.Api
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers


class FreePhotosViewModel {

    // 随手拍》提交
    fun submitFreePhotos(freePhotosVo: FreePhotosVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .submitFreePhotos(freePhotosVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<FreePhotosBean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<FreePhotosBean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "submitFreePhotos"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<FreePhotosBean>) {
                    result.eventType = "submitFreePhotos"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 随手拍》查询分页
    fun getFreePhotosPageListData(pageReq: PageReq<FreePhotosVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getFreePhotosPageListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<FreePhotosBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<FreePhotosBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = pageReq.data.seachFlag
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<FreePhotosBean>>) {
                    result.eventType = pageReq.data.seachFlag
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getFreePhotosDetailById(id: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getFreePhotosDetailById(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<FreePhotosBean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<FreePhotosBean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "getFreePhotosDetailById"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<FreePhotosBean>) {
                    result.eventType = "getFreePhotosDetailById"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun submitDealWithFreePhotos(freePhotosVo: FreePhotosVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .submitDealWithFreePhotos(freePhotosVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<Boolean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<Boolean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "submitDealWithFreePhotos"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<Boolean>) {
                    result.eventType = "submitDealWithFreePhotos"
                    EventBus.getDefault().post(result)
                }
            })
    }
}
